import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/index',
      name: 'Index',
      component: () => import('./views/index.vue'),
      children: [
        {path: '',  component: () => import('./views/Home.vue')},
        {path: '/home', name: 'Home', component: () => import('./views/Home.vue')},
        {path: '/infoShow', name: 'InfoShow', component: () => import('./views/InfoShow.vue')},
        {path: '/foundlist', name: 'FoundList', component: () => import('./views/FoundList.vue')}
      ]
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('./views/Register.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('./views/Login.vue')
    },
    {
      path: '*',
      name: 'notfound',
      component: () => import('./views/404.vue')
    }
    
  ]
})

// 添加路由守卫
router.beforeEach((to, from, next)=>{
  const isLogin = localStorage.getItem("eleToken") ? true : false
  
  if (to.path=="/login" || to.path=="/register") {
    next();
  } else {
    isLogin ? next() : next("/login")
  }


})

export default router
